import 'dart:collection';
import 'dart:convert';
import 'dart:io';

import 'package:crypto/crypto.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_menu/model/login_model.dart';
import 'package:flutter_udid/flutter_udid.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:network_info_plus/network_info_plus.dart';
import 'package:package_info_plus/package_info_plus.dart';

const String RELEASE_URL_INDONESIA = "";
const String DEBUG_URL_INDONESIA =
    "http://192.168.19.224:4523/m1/5197162-4863127-7965071d";

const String RELEASE_URL_MEXICO = "";
const String DEBUG_URL_MEXICO = "";

const String envKey = "APP_ENV";
const String debug = "debug";
const String release = "release";

const _appEnv = String.fromEnvironment(envKey);

bool isDebug() {
  return _appEnv != release;
}

class AppConfig {
  static final AppConfig _config = AppConfig._();
  final GetStorage _storage =  GetStorage();
  UserModel? loginModel;

  String? intranetIp ;
  String? language ;
  String? versionName ;
  String? SN ;
  String? android0S ;
  String? ipadOS ;
  String? osType ;
  String? deviceId;
  String? model;
  String? deviceName;
  bool afterLogin  =false;

  AppConfig._();

  static AppConfig get instance => _config;

  Future<void> init () async{
    var packageInfo = await PackageInfo.fromPlatform();
    versionName = packageInfo.version;
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    String udid  = await FlutterUdid.udid;
    SN = udid;
    intranetIp = await NetworkInfo().getWifiIP();
    if (Platform.isAndroid) {
      osType = "0";
      var info = await deviceInfo.androidInfo;
      android0S = info.version.release;
      model = info.model;
      deviceName = info.device;
      deviceId = genPesudoUniqueID([
        info.model,
        info.device,
        info.board,
        info.bootloader,
        info.device,
        info.display,
        info.fingerprint,
        info.hardware,
        info.manufacturer,
        info.serialNumber,
      ], udid);
    } else if (Platform.isIOS) {
      osType = "1";
      var info = await deviceInfo.iosInfo;
      ipadOS = info.systemName;
      model = info.model;
      deviceName = info.name;
      deviceId = genPesudoUniqueID([
        info.model,
        info.systemName,
        info.systemVersion,
        info.name,
        info.identifierForVendor ?? "",
      ], udid);
    }
  }

  EnvConfig get config  {

    if(country == Country.mexico ){
      return isDebug() ? EnvConfig.debug(url: RELEASE_URL_MEXICO) :
      EnvConfig.release(url: DEBUG_URL_MEXICO);
    }
    return isDebug() ? EnvConfig.debug(url: DEBUG_URL_INDONESIA) :
    EnvConfig.release(url: RELEASE_URL_INDONESIA);
  }

  String? get storeId => _storage.read('storeId');

  set storeId(String? storeId) {
    _storage.write("storeId", storeId);
  }

  String? get token => _storage.read('token');

  set token(String? token) {
    _storage.write("token", token);
  }

  Country? get country {
    int? index = _storage.read('country');
    return index == null ? null : Country.values[index];
  }

  set country(Country? country) {
    _storage.write("country", country?.index);
  }


  Map<String, String?> get headers {
    Map<String, String?> result = HashMap();
    result["intranetIp"] = intranetIp;
    result["language"] = language;
    result["posToken"] = token;
    result["versionName"] = versionName;
    result["SN"] = SN;
    result["Android0S"] = android0S;
    result["osType"] = osType;
    result["IpadOS"] = ipadOS;
    return result;
  }


  void logout(){
    afterLogin = false;
    loginModel= null;
  }

}
enum Country{
  indonesia("country_indonesia","corp_url","corp_phone", "RP", 0),
  mexico("country_mexico","corp_mx_url","corp_mx_phone", "\$", 2);
  final String title;
  final String  url ;
  final String phone;
  final String unit;
  final int decimalDigits;

  const Country(this.title,this.url,this.phone, this.unit, this.decimalDigits);
}

class EnvConfig {

  final String title;
  final String url;

  const EnvConfig(this.title, this.url);

  factory EnvConfig.debug({String url = DEBUG_URL_INDONESIA}){
    return EnvConfig("测试环境",url);
  }
  factory EnvConfig.release({String url = RELEASE_URL_INDONESIA}){
    return EnvConfig("正式环境",url);
  }

}

String genPesudoUniqueID(List<String> strings, String uuid) {
  StringBuffer buffer = StringBuffer();
  buffer.write(uuid);
  buffer.write("35");
  for (String e in strings) {
    buffer.write(e.length % 10);
  }

  final content = utf8.encode(buffer.toString());
  final digest = md5.convert(content);
  return digest.toString();
}